package com.hcms.admin.controller.cms;

import cn.hutool.core.bean.BeanUtil;
import com.hcms.admin.dto.SysAdminResponse;
import com.hcms.admin.service.ICmsArticleService;
import com.hcms.common.api.CommonPage;
import com.hcms.common.api.CommonResponse;
import com.hcms.mybatis.entity.CmsArticle;
import com.hcms.mybatis.entity.SysAdmin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 文章 前端控制器
 * </p>
 *
 * @author achenxw@126.com
 * @since 2023-03-30
 */
@RestController
@RequestMapping("/cmsArticle")
@Api
public class CmsArticleController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ICmsArticleService cmsArticleService;

    @GetMapping("")
    @ResponseBody
    public ResponseEntity<CommonResponse<CommonPage<CmsArticle>>> index(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        logger.info("Run index(), pageNum={}, pageSize={}", pageNum, pageSize);
        List<CmsArticle> list = cmsArticleService.getList(pageNum, pageSize);
        return CommonResponse.ok(CommonPage.restPage(list));
    }

    @ApiOperation("获取指定用户信息")
    @GetMapping("/{id}")
    @ResponseBody
    public ResponseEntity<CommonResponse<SysAdminResponse>> view(@PathVariable Long id) {
        logger.info("Obtaining user details, id={}", id);
        CmsArticle item = cmsArticleService.getView(id);
        if (item == null) {
            logger.error("The user does not exist");
            return CommonResponse.notFound("记录不存在");
        }
        SysAdminResponse result = BeanUtil.copyProperties(item, SysAdminResponse.class);
        logger.info("Obtaining user details succeeded");
        return CommonResponse.ok(result);
    }

    @ApiOperation("添加用户")
    @PostMapping("")
    @ResponseBody
    public ResponseEntity<CommonResponse<CmsArticle>> create(@RequestBody CmsArticle entity) {
        logger.info("Run create()");
        int count = cmsArticleService.createItem(entity);
        if (count > 0) {
            logger.info("Adding succeeded, id={}", entity.getId());
            return CommonResponse.created(entity);
        } else {
            logger.error("Adding failed");
            return CommonResponse.badRequest("添加失败");
        }
    }

    @ApiOperation("更新用户信息")
    @PutMapping("/{id}")
    @ResponseBody
    public ResponseEntity<CommonResponse<SysAdmin>> update(@PathVariable Long id, @RequestBody CmsArticle entity) {
        logger.info("Run update(), id={}", id);
        int count = cmsArticleService.updateItem(id, entity);
        if (count > 0) {
            logger.info("Update succeeded");
            return CommonResponse.accepted("更新成功");
        } else {
            logger.error("Update failed");
            return CommonResponse.badRequest("更新失败");
        }
    }

    @ApiOperation("删除记录")
    @DeleteMapping("/{ids}")
    @ResponseBody
    public ResponseEntity<CommonResponse<Object>> delete(@PathVariable String ids) {
        logger.info("Run delete(), ids={}", ids);
        int count = cmsArticleService.deleteItem(ids);
        if (count > 0) {
            logger.info("Delete succeeded");
            return CommonResponse.noContent("删除成功");
        } else {
            logger.info("Delete failed");
            return CommonResponse.badRequest("删除失败");
        }
    }

}
